package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.entity.Addresses;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Users;
import com.ruoyi.common.core.domain.model.LoginAppUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.web.service.AppTokenService;
import com.ruoyi.system.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/app/user/profile")
public class AppProfileController extends BaseController {
    @Autowired
    private IUsersService userService;
    @Autowired
    private AppTokenService appTokenService;
    /**
     * 个人信息
     */
    @GetMapping
    public AjaxResult profile()
    {
        LoginAppUser loginAppUser = getLoginAppUser();
        Users user = loginAppUser.getAppUser();
        return AjaxResult.success(user);
    }
    /**
     * 头像上传
     */
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            LoginAppUser loginAppUser = getLoginAppUser();
            String userLogo = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (userService.updateUserAvatar(loginAppUser.getUsername(), userLogo))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", userLogo);
                // 更新缓存用户头像
                loginAppUser.getAppUser().setUserLogo(userLogo);
                appTokenService.setLoginAppUser(loginAppUser);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }
    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult updateProfile(@RequestBody Users user)
    {
        LoginAppUser loginAppUser = getLoginAppUser();
        Users currentUser = loginAppUser.getAppUser();
        currentUser.setUserName(user.getUserName());
        currentUser.setUserEmail(user.getUserEmail());
        currentUser.setUserPhone(user.getUserPhone());
        if (userService.updateUserProfile(currentUser) > 0)
        {
            // 更新缓存用户信息
            appTokenService.setLoginAppUser(loginAppUser);
            return success();
        }
        return error("修改个人信息异常，请联系管理员");
    }
    /**
     * 查询用户地址列表
     */
    @PutMapping("/address")
    public List<Addresses> list(@RequestBody Addresses addresses)
    {
        startPage();
        return userService.selectAddressesList(addresses);
    }
    /**
     * 获取用户地址详细信息
     */
    @PutMapping("/addressId")
    public AjaxResult getInfo(Long addressId) {
        return success(userService.selectAddressesByAddressId(addressId));
    }
    /**
     * 修改用户地址
     */
    @PutMapping("/editAddress")
    public AjaxResult edit(@RequestBody Addresses addresses)
    {
        return toAjax(userService.updateAddresses(addresses));
    }
    /**
     * 新增用户地址
     */
    @PostMapping("/addAddress")
    public AjaxResult add(@RequestBody Addresses addresses)
    {
        return toAjax(userService.insertAddresses(addresses));
    }
}
